package com.ecas.code.generator.util;


import com.ecas.code.generator.constent.ServletConstent;

import com.ecas.code.generator.enums.GenerateTypeEnum;
import com.google.common.collect.Lists;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CodeGeneratorFeemarkerUtil {
    private static final Logger LOG = LoggerFactory.getLogger(CodeGeneratorFeemarkerUtil.class);

    private CodeGeneratorFeemarkerUtil() {
    }

    public static void codeGenerator(Properties properties, Map templateData, ZipOutputStream zipOutputStream) {

        dealTemplateConfig(templateData, properties);

        Version version = new Version(2, 3, 27, null, true, new Date());
        String templatePathStr = (String) properties.get("template.path");

        templatePathStr = System.getProperty("user.dir")  + File.separator+ templatePathStr;
        File templatePath = new File(templatePathStr);
        List<String> tempalteNameList = getTemplateNameList(properties.get("stuffx").toString());
        try {
            for (String templateName : tempalteNameList) {
                Configuration configuration = new Configuration(version);
                configuration.setDefaultEncoding(ServletConstent.UTF_8);
                configuration.setLogTemplateExceptions(true);
                configuration.setClassicCompatible(true);

                configuration.setDirectoryForTemplateLoading(templatePath);
                Template template = configuration.getTemplate(templateName);
                StringWriter stringWriter = new StringWriter();
                template.process(templateData, stringWriter);

                ZipEntry zipEntry = new ZipEntry(properties.get("fileName")+templateName.replace(".ftl",properties.get("stuffx").toString()));
                zipOutputStream.putNextEntry(zipEntry);
                IOUtils.write(stringWriter.toString(), zipOutputStream,"utf-8");
                stringWriter.flush();
                stringWriter.close();

            }

        } catch (IOException e) {
            LOG.error("codeGenerator,is IOException:", e);
        } catch (TemplateException e) {
            LOG.error("codeGenerator,is error tempalteName:{} TemplateException:", templatePath, e);
        }
    }
    public static void protoGenerate(Properties properties, Map templateData, ZipOutputStream zipOutputStream){

    }

    private static void dealTemplateConfig(Map templateData, Properties configPropertie) {
        templateData.put("appName", configPropertie.get("appName"));
        templateData.put("basePackage", configPropertie.get("basePackage"));
        templateData.put("author", configPropertie.get("author"));
        templateData.put("email", configPropertie.get("email"));
    }


    private static List<String> getTemplateNameList(String stuffx) {
        List list = Lists.newArrayList();
        if(stuffx.equals(".java")) {
            list.add("Controller.ftl");
            list.add("Service.ftl");
            list.add("ServiceImpl.ftl");
            list.add("Manager.ftl");
            list.add("Dao.ftl");
            list.add("Entiy.ftl");
        }else if(stuffx.equals(".proto")){
            list.add("Proto.ftl");
        }else  if(stuffx.equals(".html")){
            list.add("html5.ftl");
        }

        return list;
    }


    private static String getFileName(String templateName, Map templateData) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(templateData.get("moduleName"));
        stringBuilder.append(File.separator);
        stringBuilder.append(templateData.get("className"));
        if(templateName.contains("Proto")){
            stringBuilder.append(templateName.replace("ftl", GenerateTypeEnum.PROTO.getSuffix()));
        }else if (templateName.contains("js")){
            stringBuilder.append(templateName.replace("ftl", GenerateTypeEnum.JAVASCRIPT.getSuffix()));
        }else if(templateName.contains("html")){
            stringBuilder.append(templateName.replace("ftl", GenerateTypeEnum.HTML5.getSuffix()));
        }else {
            stringBuilder.append(templateName.replace("ftl", GenerateTypeEnum.JAVA.getSuffix()));
        }
        return stringBuilder.toString();
    }

}


